package com.vancouver.knows.ui.fragment.citydiscount;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import butterknife.Bind;
import butterknife.ButterKnife;
import me.everything.android.ui.overscroll.HorizontalOverScrollBounceEffectDecorator;
import me.everything.android.ui.overscroll.IOverScrollDecor;
import me.everything.android.ui.overscroll.IOverScrollState;
import me.everything.android.ui.overscroll.IOverScrollStateListener;
import me.everything.android.ui.overscroll.IOverScrollUpdateListener;
import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;
import me.everything.android.ui.overscroll.adapters.ViewPagerOverScrollDecorAdapter;

import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.ImageViewPagerActivity;
import com.vancouver.knows.util.AppUtils;

public class BannerViewControll {

	private Activity activity;

	@Bind(R.id.viewpager)
	ViewPager viewPager;
	@Bind(R.id.point_group)
	LinearLayout pointGroup;
	@Bind(R.id.right_reveal_panel)
	LinearLayout rightRevealPanel;
	@Bind(R.id.left_reveal_panel)
	LinearLayout leftRevealPanel;

	private ArrayList<String> datas = new ArrayList<String>();
	private ArrayList<ImageView> imageList = new ArrayList<ImageView>();
	private MyPagerAdapter myPagerAdapter;
	/**
	 * 上一个页面的位置
	 */
	protected int lastPosition;

	@SuppressWarnings("deprecation")
	public BannerViewControll(final Activity activity, View view, Object listener) {
		this.activity = activity;
		ButterKnife.bind(this, view);
		myPagerAdapter = new MyPagerAdapter();
		viewPager.setAdapter(myPagerAdapter);
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
			/**
			 * 页面切换后调用 position 新的页面位置
			 */
			@Override
			public void onPageSelected(int position) {
				// 改变指示点的状态
				// 把当前点enbale 为true
				pointGroup.getChildAt(position).setEnabled(true);
				// 把上一个点设为false
				pointGroup.getChildAt(lastPosition).setEnabled(false);
				lastPosition = position;
				Picasso.with(activity).load(datas.get(position)).placeholder(R.drawable.no_image)
						.into(imageList.get(position));
			}

			/**
			 * 页面正在滑动的时候，回调
			 */
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			/**
			 * 当页面状态发生变化的时候，回调
			 */
			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	public void updateView(List<String> images) {
		if (images == null || images.size() == 0) {
			datas = new ArrayList<String>();
		} else {
			datas.clear();
			datas.addAll(images);
		}

		pointGroup.removeAllViews();
		for (int i = 0; i < datas.size(); i++) {
			// 初始化图片资源
			ImageView image = new ImageView(activity);
			imageList.add(image);
			// 添加指示点
			ImageView pointImg = new ImageView(activity);
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			params.rightMargin = 5;
			pointImg.setLayoutParams(params);
			pointImg.setBackgroundResource(R.drawable.point_bg);
			if (i == 0) {
				pointImg.setEnabled(true);
			} else {
				pointImg.setEnabled(false);
			}
			pointGroup.addView(pointImg);
		}

		myPagerAdapter.notifyDataSetChanged();
	}

	public void setOverScroll(Object listener) {
		HorizontalOverScrollBounceEffectDecorator overScrollDecorator = new HorizontalOverScrollBounceEffectDecorator(new ViewPagerOverScrollDecorAdapter(viewPager), 1f, 2f, -1f);
		overScrollDecorator.setOverScrollUpdateListener((IOverScrollUpdateListener) listener);
		overScrollDecorator.setOverScrollStateListener((IOverScrollStateListener) listener);
	}

	private class MyPagerAdapter extends PagerAdapter {

		/**
		 * 获得页面的总数
		 */
		@Override
		public int getCount() {
			return datas.size();
		}

		/**
		 * 获得相应位置上的view container view的容器，其实就是viewpager自身 position 相应的位置
		 */
		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			// 给 container 添加一个view
			final ArrayList<String> urls = datas;
			String url = datas.get(position);
			imageList.get(position).setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(activity, ImageViewPagerActivity.class);
					intent.putStringArrayListExtra("urls", urls);
					intent.putExtra("index", position);
					activity.startActivity(intent);
				}
			});
			container.addView(imageList.get(position));
			Picasso.with(activity).load(url).placeholder(R.drawable.no_image).resize(AppUtils.getWindowWidth(activity), 0).into(imageList.get(position));
			// 返回一个和该view相对的object
			return imageList.get(position);
		}

		/**
		 * 判断 view和object的对应关系
		 */
		@Override
		public boolean isViewFromObject(View view, Object object) {
			if (view == object) {
				return true;
			} else {
				return false;
			}
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		/**
		 * 销毁对应位置上的object
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
			object = null;
		}
	}

}
